package reflect;

import java.lang.reflect.Method;
import java.util.Scanner;

public class ReflectDemo4 {
    public static void main(String[] args) throws Exception {
        Person p = new Person();
        p.sayHello();


        //获取Class对象
//        Class cls = Class.forName("reflect.Person");
//        //创建对象,实例化
//        Object obj = cls.newInstance();
//
//        Method m = cls.getMethod("sayHello");
//        m.invoke(obj);//obj.sayHello()

        Scanner sc = new Scanner(System.in);
        System.out.println("请输入类名：");
        String className = sc.nextLine();
        System.out.println("请输入方法名：");
        String methodName = sc.nextLine();
        Class cls = Class.forName(className);
        Object obj = cls.newInstance();
        Method m = cls.getMethod(methodName);
        m.invoke(obj);
    }
}
